package xjtu.se.articlecommunity.common.impl;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.se.articlecommunity.common.MailService;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

@Service
@Transactional
public class MailServiceImpl implements MailService {

    @Resource
    private JavaMailSender javaMailSender;

    public static String emailAccount = "xjtuse_wzcom2021@163.com";
    public static String emailSMTPHost = "smtp.163.com";

    @Override
    public void sendMail(String to, String subject, String text) {
        try{
            Session session = configMail();
            MimeMessage message = createMimeMessage(session, emailAccount, to, subject, text);
            javaMailSender.send(message);
        } catch (UnsupportedEncodingException | MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String sendCode(String to) {

        String code = String.valueOf(new Random().nextInt(899999) + 100000);
        String subject = "验证码："+ code;
        String html = "<html><body>" +
                "<p>尊敬的用户：</p>" +
                "<p style = \"text-indent:2em\">您好！感谢您使用文章社区服务，您正在进行邮箱验证，本次请求的验证码为：" + code +
                "。</p><p>为了保障您帐号的安全性，请在5分钟内完成验证。</p>" +
                "<p style = \"text-indent:2em\">愿您在社区开启一段有趣、有益的文章之旅。</p>" +
                "<p style = \"text-indent:36em\">文章社区开发团队&nbsp;&nbsp;</p>" +
                "</body></html>";
        sendMail(to, subject, html);
        return code;
    }

    @Override
    public void sendRegistrationSuccessNotification(String to) {
        String subject = "注册申请通过";
        String html = "<html><body>" +
                "<p>尊敬的用户：</p>" +
                "<p style = \"text-indent:2em\">您好！您的文章社区后台系统账号注册申请成功！" +
                "</p><p>现在您可以通过密码/验证码登录您的账号。</p>" +
                "<p style = \"text-indent:2em\">愿您在社区开启一段有趣、有益的文章之旅。</p>" +
                "<p style = \"text-indent:16em\">文章社区开发团队&nbsp;&nbsp;</p>" +
                "</body></html>";
        sendMail(to, subject, html);
    }

    private Session configMail(){

        Properties props = new Properties();
        props.setProperty("mail.debug", "true");
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.host", emailSMTPHost);
        props.setProperty("mail.transport.protocol", "smtp");

        Session session = Session.getInstance(props);
        session.setDebug(true);

        return session;
    }

    public static MimeMessage createMimeMessage(Session session, String from, String to,
                                                String subject, String html) throws MessagingException, IOException {

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from, "文章社区", "UTF-8"));
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
        message.setSubject(subject, "UTF-8");
        message.setContent(html, "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        message.saveChanges();
        return message;
    }
}
